///******************** NG.Respate(C)  **********************************
//* 产品名称: Respate 科技
//* 文件名称: select.c
//* 程序作者: 雷帕特公司(南宫子萍)
//* 程序版本: V1.1														   
//* 编制日期: 2019/1/29
//* 功能描述: 系统日志
//* QQ:	2085827949
//**********************************************************************/

//#include "lap_lib.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>



//多路io复用
void lap_select(void)
{
	char buff[50] = {0};
	int ret =-1;
	fd_set devfds={0};
	
	//打开鼠标标准输入
	int mousefd =  -1;
	mousefd = open("/dev/input/mouse0",O_RDONLY);
	if(mousefd<0)
	{
		printf("open mouse0 err!\n");
		return ;
	}
	
	
	struct timeval out_time;
	out_time.tv_sec = 5;
    out_time.tv_usec = 0;

	
	while(1)
	{
		//设置 阻塞超时时间
		out_time.tv_sec = 5;
		
		FD_ZERO(&devfds);		//清空设备描述符
		FD_SET(0,&devfds);		//加入设备描述符
		FD_SET(mousefd,&devfds);//加入设备描述符
		
		//注册多路复用IO 设备描述符
		ret = select(mousefd+1,&devfds,NULL,NULL,&out_time);
		
		//判断哪路IO到来	
		if(ret == 0)
		{
			printf("IO time out !\n");
		}
		else if(ret > 0)
		{
			if(FD_ISSET(mousefd,&devfds))	//鼠标输入
			{
				memset(buff,0,sizeof(buff));		//清空缓冲区
				read(mousefd,buff,sizeof(buff));	//读取鼠标输入字符
				printf("鼠标输入字节：%s",buff);			//打印读取的字符串
			}
			if(FD_ISSET(0,&devfds))			//键盘输入
			{
				memset(buff,0,sizeof(buff));		//清空缓冲区
				read(0,buff,sizeof(buff));			//读取键盘输入字符
				printf("鼠标输入字节：%s",buff);			//打印读取的字符串
			}
		}
	}
	
}




#include <poll.h>
void lap_poll(void)
{
	char buff[50] = {0};
	int ret =-1;
	struct pollfd fds[2];
	
	//打开鼠标标准输入
	int fd = -1;
	fd = open("/dev/input/mouse0",O_RDONLY);
	if( fd < 0)
	{
		printf("mouse0 open err\n");
		return ;
	}

	//填充设备描述符
	fds[0].fd	=	0;
	fds[0].events =  POLLIN;
	
	fds[1].fd	=	fd;
	fds[1].events =  POLLIN;

	
	while(1)
	{
		ret = poll(fds,fd+1,5000);
		if(ret==0)
		{
			printf("poll time oout\n");
		}
		else if(ret >0)
		{
			if(fds[1].revents & POLLIN)	//鼠标输入
			{
				memset(buff,0,sizeof(buff));		//清空缓冲区
				read(fds[1].fd,buff,sizeof(buff));	//读取鼠标输入字符
				printf("鼠标输入字节：%s",buff);			//打印读取的字符串
			}
			if(fds[0].revents & POLLIN)			//键盘输入
			{
				memset(buff,0,sizeof(buff));		//清空缓冲区
				read(fds[0].fd,buff,sizeof(buff));			//读取键盘输入字符
				printf("鼠标输入字节：%s",buff);			//打印读取的字符串
			}
		}
	}
}





#if 0	//功能测试

int main(void)
{
	
	lap_select();
//	lap_poll();

	
	return 0;
}

#endif







																						 
